package com.hdjk.view.callback;

import com.hdjk.view.entity.BankEntity;
import com.hdjk.view.entity.PayMethodEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class GetPayMethodCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONObject object = (JSONObject) msg.getData();
        if (object!=null) {
            PayMethodEntity entity = new PayMethodEntity();
            if (!object.isNull("investorBankDetails")) {
                JSONObject inv = object.getJSONObject("investorBankDetails");
                PayMethodEntity.InvestorBankDetails bankDetails = new PayMethodEntity().new InvestorBankDetails();
                if (!inv.isNull("data")) {
                    JSONArray banks = inv.getJSONArray("data");
                    List<BankEntity> bankList = new ArrayList<>();
                    for (int j = 0; j < banks.length(); j++) {
                        JSONObject b = banks.getJSONObject(j);
                        BankEntity bank = new BankEntity();
                        if (!b.isNull("investorPayId")) {
                            bank.setInvestorPayId(b.getString("investorPayId"));
                        }
                        if (!b.isNull("bankCode")) {
                            bank.setBankCode(b.getString("bankCode"));
                        }
                        if (!b.isNull("bankNumber")) {
                            bank.setBankNumber(b.getString("bankNumber"));
                        }
                        if (!b.isNull("preferred")) {
                            bank.setPreferred(b.getString("preferred"));
                        }
                        bankList.add(bank);
                    }
                    bankDetails.setData(bankList);
                }
                entity.setInvestorBankDetails(bankDetails);
            }
            if (!object.isNull("investorCashWallets")) {
                JSONObject inv = object.getJSONObject("investorCashWallets");
                PayMethodEntity.InvestorCashWallets wallets = new PayMethodEntity().new InvestorCashWallets();
                if (!inv.isNull("data")) {
                    JSONArray wa = inv.getJSONArray("data");
                    List<PayMethodEntity.Wallet> walletList = new ArrayList<>();
                    for (int j = 0; j < wa.length(); j++) {
                        JSONObject b = wa.getJSONObject(i);
                        PayMethodEntity.Wallet wallet = new PayMethodEntity().new Wallet();
                        if (!b.isNull("availableUnits")) {
                            wallet.setAvailableUnits(b.getString("availableUnits"));
                        }
                        if (!b.isNull("fundCode")) {
                            wallet.setFundCode(b.getString("fundCode"));
                        }
                        if (!b.isNull("fundName")) {
                            wallet.setFundName(b.getString("fundName"));
                        }
                        if (!b.isNull("preferred")) {
                            wallet.setPreferred(b.getString("preferred"));
                        }
                        walletList.add(wallet);
                    }
                    wallets.setData(walletList);
                }
                entity.setInvestorCashWallets(wallets);
            }
            msg.setData(entity);
        }
        return msg;
    }
}
